#ifndef KNOWLEDGE_UNIT_H
#define KNOWLEDGE_UNIT_H

#include <string>
#include <vector>
#include "src/symbols/wubi/wubi_symbol.h"

class KnowledgeUnit {
protected:
    std::string id;
    std::vector<WuBiSymbol> associatedSymbols;
    double activationThreshold;
    double currentActivation;
    double errorRate;
    double processingTime;

public:
    KnowledgeUnit(const std::string& knowledgeId) 
        : id(knowledgeId), activationThreshold(0.5), currentActivation(0.0),
          errorRate(0.0), processingTime(0.0) {}
    
    virtual ~KnowledgeUnit() = default;
    
    // 判断是否应该被激活
    virtual bool shouldBeActivated(const WuBiSymbol& symbol) {
        // 基于语义相似度判断
        for (const auto& associated : associatedSymbols) {
            if (symbol.similarity(associated) > activationThreshold) {
                return true;
            }
        }
        return false;
    }
    
    // 激活知识单元
    virtual void activate() {
        currentActivation = 1.0;
        onActivate();
    }
    
    // 知识单元被激活时的回调
    virtual void onActivate() = 0;
    
    // 纠正知识单元
    virtual void correct() {
        errorRate *= 0.5;  // 简化实现，实际应用中需要更复杂的纠正逻辑
    }
    
    // 优化知识单元
    virtual void optimize() {
        processingTime *= 0.8;  // 简化实现，实际应用中需要更复杂的优化逻辑
    }
    
    // 扩展知识单元
    virtual void expand() {
        // 简化实现，实际应用中需要更复杂的扩展逻辑
    }
    
    // 获取错误率
    double getErrorRate() const { return errorRate; }
    
    // 获取处理时间
    double getProcessingTime() const { return processingTime; }
};

// 推理规则基类
class InferenceRule {
public:
    virtual ~InferenceRule() = default;
    
    // 判断规则是否可以应用
    virtual bool canApply(const std::vector<WuBiSymbol>& symbols,
                         const std::set<KnowledgeUnit*>& knowledge) = 0;
    
    // 应用规则
    virtual SemanticReasoningResult apply(const std::vector<WuBiSymbol>& symbols,
                                        const std::set<KnowledgeUnit*>& knowledge) = 0;
};

// 性能评估指标
struct PerformanceMetrics {
    double accuracy;
    double efficiency;
    double adaptability;
    
    PerformanceMetrics() : accuracy(0.0), efficiency(0.0), adaptability(0.0) {}
};

#endif // KNOWLEDGE_UNIT_H